<extend name="layout/edit" />

<block name="HEADER">

</block>

<block name="CONTENT">
    <control:form id="form"/>
</block>

<block name="FOOTER">
   <script type="text/javascript">
    var  page=(function(ns){
    	
     	
		ns.init = function(){
			
			if(window.__mode!=1){
				form.setReadonly('shop_id',false,false);  
				form.setReadonly('pid',false,false);  
			}
			
			form.on("input",'shop_id',function(e){ ns.addOption(); });
			ns.addOption();
		}
		
		ns.addOption = function(){
			$.ajaxs('/admin/v1/goods_cate/xmlGoodsParentCate',{
    			shop_id: form.getValue('shop_id')
    		}).then(function(res){
    			var pid = form.getValue('pids');
    			form.addOption('pid',res.data);
    			form.setValue('pid',pid);
    		});
		}
		
		//新增行
		ns.addRow = function(){
			form.$grids['levelGrid'].addRow([{
				level:'',
				name:'',
				discounts:'',
				commission_one:'',
				commission_two:'',
			}]);
		}
		
		ns.deleteRow = function(){
			form.$grids['levelGrid'].deleteRow();    
		}
        
        //保存
        ns.save=function(){
        	form.save(4).then(function(res){
        		$.ajaxs('/admin/v1/goods_level/add',{
        			cate_id:res.data,
        			data:JSON.stringify(form.$grids['levelGrid'].getRows().map(function(h){
        				delete h.id;
        				h.cate_id = res.data;
        				return h;
        			}))
        		}).then(function(res){
        			form.opener.grid.load();
        			form.opener.$.toast(1,"成功！");
        			$.close();
        		});
    		});
        }
        return ns;
    }({}));
    
    $(function(){ page.init(); });
   </script>
</block>



